﻿using Microsoft.Xna.Framework;

namespace block_explorer
{
    class LookatCamera : Camera
    {
        private float mViewWidth;
        private float mViewHeight;
        private float mFOV;
        private float mNear;
        private float mFar;

        private Vector3 mPosition;
        private Vector3 mLookat;

        public float Near { get { return mNear; } }
        public float Far { get { return mFar; } }
        public float AspectRatio { get { return mViewWidth / mViewHeight; } }
        public Vector3 Position { get { return mPosition; } set { mPosition = value; UpdateMatrices(); } }
        public Vector3 LookAt { get { return mLookat; } set { mLookat = value; UpdateMatrices(); } }

        public Vector3 Direction { get { return mLookat - mPosition; } }

        public LookatCamera(Vector3 position, Vector3 lookat, float viewWidth, float viewHeight, float fov, float near, float far)
        {
            mPosition = position;
            mLookat = lookat;

            mViewWidth = viewWidth;
            mViewHeight = viewHeight;
            mFOV = fov;
            mNear = near;
            mFar = far;

            UpdateMatrices();
        }

        private void UpdateMatrices()
        {
            Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(mFOV / 2.0f), mViewWidth / mViewHeight, mNear, mFar);
            View = Matrix.CreateLookAt(mPosition, mLookat, new Vector3(0, 1, 0));
        }
    }
}
